/**
 * 2011 HuYao
 */
package com.wh.data.pp.bp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.wh.data.model.SumBp;
import com.wh.data.model.SumBpLb;
import com.wh.data.model.SumBpWLunch;

/**
 * @author HuYao
 * 
 */
public class SumBpCheckerWSumBpLb {
    private String bplbFilePath = ""; //$NON-NLS-1$
    private String bpFilePath = ""; //$NON-NLS-1$

    final private List<SumBp> sumBpList = new ArrayList<SumBp>();
    final private List<SumBpLb> sumBpLbList = new ArrayList<SumBpLb>();

    /**
     * @param sum_bp_path
     * @param sum_bplb_path
     * @throws IOException
     */
    public SumBpCheckerWSumBpLb(String sum_bp_path, String sum_bplb_path)
	    throws IOException {
	this.bplbFilePath = sum_bplb_path;
	this.bpFilePath = sum_bp_path;

	File bpFile = new File(this.bpFilePath);
	if (!bpFile.exists())
	    throw new FileNotFoundException();
	BufferedReader br = new BufferedReader(new FileReader(bpFile));
	String line = ""; //$NON-NLS-1$
	while ((line = br.readLine()) != null) {
	    StringTokenizer st = new StringTokenizer(line);
	    try {
		int therapist = Integer.parseInt(st.nextToken());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		double total_cost = Double.parseDouble(st.nextToken());
		double total_time = Double.parseDouble(st.nextToken());
		double distance = Double.parseDouble(st.nextToken());
		int day = Integer.parseInt(st.nextToken());
		SumBp tmp = new SumBp(therapist, start, end, total_cost,
			total_time, distance, day);
		this.sumBpList.add(tmp);
		tmp = null;
	    } catch (NumberFormatException e) {
		System.out.println("Skip this line:" + line); //$NON-NLS-1$
	    }
	}
	br.close();
	br = null;

	File bplbFile = new File(this.bplbFilePath);
	if (!bplbFile.exists())
	    throw new FileNotFoundException();
	br = new BufferedReader(new FileReader(bplbFile));
	line = ""; //$NON-NLS-1$
	while ((line = br.readLine()) != null) {
	    StringTokenizer st = new StringTokenizer(line);
	    try {
		int therapist = Integer.parseInt(st.nextToken());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		int day = Integer.parseInt(st.nextToken());
		SumBpLb tmp = new SumBpLb(therapist, start, end, day);
		this.sumBpLbList.add(tmp);
		tmp = null;
	    } catch (NumberFormatException e) {
		System.out.println("Skip this line:" + line); //$NON-NLS-1$
	    }
	}
	br.close();
    }

    /**
     * @throws IOException
     */
    public void process() throws IOException {

	List<SumBpWLunch> sumbpWLunList = new ArrayList<SumBpWLunch>();
	for (SumBp sumBp : this.sumBpList) {
	    SumBpWLunch tmp = new SumBpWLunch(sumBp.getTherapist(),
		    sumBp.getStart(), sumBp.getEnd(), sumBp.getTotal_cost(),
		    sumBp.getTotal_time(), sumBp.get_distance(),
		    sumBp.getDay(), this.sumBpLbList.contains(sumBp) ? 1 : 0);
	    sumbpWLunList.add(tmp);
	    String dbline = tmp.getTherapist() + " " + tmp.getStart() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + tmp.getEnd() + " " + tmp.getTotal_cost() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + tmp.getTotal_time() + " " + tmp.get_distance() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + tmp.getDay() + " " + tmp.get_lunch(); //$NON-NLS-1$
	    System.out.println(dbline);
	    tmp = null;
	}
	File fpFile = new File(this.bpFilePath);
	BufferedWriter bw = new BufferedWriter(new FileWriter(fpFile));
	for (SumBpWLunch sumbpWlun : sumbpWLunList) {
	    String line = sumbpWlun.getTherapist()
		    + " " + sumbpWlun.getStart() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbpWlun.getEnd()
		    + " " + sumbpWlun.getTotal_cost() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbpWlun.getTotal_time()
		    + " " + sumbpWlun.get_distance() + " " //$NON-NLS-1$ //$NON-NLS-2$
		    + sumbpWlun.getDay() + " " + sumbpWlun.get_lunch(); //$NON-NLS-1$
	    bw.write(line);
	    bw.newLine();
	}
	bw.close();
    }
}
